<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>
    500 Internal Server Error (trying to upload file) : Forums : PythonAnywhere
</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="Forums : PythonAnywhere">
        <meta name="author" content="PythonAnywhere LLP">
        <meta name="google-site-verification" content="O4UxDrfcHjC44jybs2vajc1GgRkTKCTRgVzeV6I9V14" />

        <!-- Le styles -->
        <link href="/static/bootstrap/css/bootstrap.css" rel="stylesheet">
        <link href="/static/bootstrap/css/bootstrap-responsive.css" rel="stylesheet">
        <link href="/static/anywhere/styles/bootstrap_base.css" rel="stylesheet">
        
    
    <link rel="stylesheet" href="/static/anywhere/styles/forums.css" type="text/css" media="screen" charset="utf-8" />

    <link rel="stylesheet" href="/static/pygments/style.css" type="text/css" media="screen" charset="utf-8" />

        <link rel="stylesheet" href="/static/jquery/jquery-ui-1.8.11.custom.css" type="text/css" media="screen" charset="utf-8" />
        
    
    <link rel="alternate" type="application/rss+xml" title="RSS" href="/forums/topic/828/rss" />

        <style type="text/css">
            body {
                height: auto;
            }
        </style>
        <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
        <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->

        <!-- Le fav and touch icons -->
        <link rel="apple-touch-icon" href="images/apple-touch-icon.png">
        <link rel="apple-touch-icon" sizes="72x72" href="images/apple-touch-icon-72x72.png">
        <link rel="apple-touch-icon" sizes="114x114" href="images/apple-touch-icon-114x114.png">
    </head>

     <body>
        <div class="navbar navbar-fixed-top">
            <div class="navbar-inner">
                <div id="id_internal_nav_bar_container" class="container">
                    <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    </a>
                    <a class="brand" href="/"><img id="id_logo" src="/static/anywhere/images/logo-234x35.png" height="35" title="PythonAnywhere logo" alt="PythonAnywhere logo" /></a>
                    <div class="nav-collapse">
                        <ul id="id_header_links" class="nav">
                            <li><a id="id_feedback_link" class='feedback_link' href="">Send feedback</a></li>
                            <li><a id="id_forums_link" href="/forums/">Forums</a></li>
                            <li><a href="/wiki/" id="id_help_link">Help</a></li>
                            <li><a href="http://blog.pythonanywhere.com/" id="id_blog_link">Blog</a></li>
                            
                            
                                <li><a style="font-weight: bold;" href="/pricing/" id="id_pricing_link">Pricing & signup</a></li>
                                <li><a href="/login/?next=/forums/topic/828/" id="id_login_link">Log in</a></li>
                            
                        </ul>
                    </div>
                </div>
            </div>
        </div>

        <div class="top-container" id="id_dummy"></div>

        

        
    
    <div class="container">
        <div class="row">
            <div class="span8 offset3">
                <h1>PythonAnywhere Forums</h1>
            </div>
        </div>



    <div class="row">
        <div class="span8 offset3">
            <p>
                <a href="/forums/start_topic/" id="id_start_new_topic">Start a new topic</a> |
                <a href="/forums/">Back to all topics</a> |
                <a href="/forums/topic/828/rss">This topic's RSS</a>
            </p>
        </div>
    </div>
    <div class="row">
        <div class="span8 offset3 topic">
            <h2>500 Internal Server Error (trying to upload file)</h2>
        </div>
    </div>

    
        <div class="row">
            <div id="id_post_5871" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5871_content_text" class="span8">
                        <p>Hi, I'm completely new to web application development. I followed the Flask tutorial for uploading files (http://flask.pocoo.org/docs/patterns/fileuploads/). I copied the code exactly except for changing the UPLOAD_FOLDER path. When I try uploading a file I get 500 error. Am I writing the path wrong or is something else wrong? Thank you.</p>
<hr />
<div class="codehilite"><pre><span class="k">import</span> <span class="nx">os</span>
<span class="nb">from</span> <span class="nx">flask</span> <span class="k">import</span> <span class="nx">Flask</span><span class="p">,</span> <span class="nx">request</span><span class="p">,</span> <span class="nx">redirect</span><span class="p">,</span> <span class="nx">url_for</span>
<span class="nb">from</span> <span class="nx">werkzeug</span> <span class="k">import</span> <span class="nx">secure_filename</span>

<span class="n">UPLOAD_FOLDER</span> <span class="o">=</span> <span class="s1">&#39;/home/jwebapp3/mysite&#39;</span>
<span class="n">ALLOWED_EXTENSIONS</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="err">[</span><span class="s1">&#39;txt&#39;</span><span class="p">,</span> <span class="s1">&#39;pdf&#39;</span><span class="p">,</span> <span class="s1">&#39;png&#39;</span><span class="p">,</span> <span class="s1">&#39;jpg&#39;</span><span class="p">,</span> <span class="s1">&#39;jpeg&#39;</span><span class="p">,</span> <span class="s1">&#39;gif&#39;</span><span class="cp">]</span>)

app = Flask(__name__)
 app.config<span class="cp">[</span><span class="s1">&#39;UPLOAD_FOLDER&#39;</span><span class="cp">]</span> = UPLOAD_FOLDER

def allowed_file(filename):
    return &#39;.&#39; in filename and \
           filename.rsplit(&#39;.&#39;, 1)<span class="cp">[</span><span class="mi">1</span><span class="cp">]</span> in ALLOWED_EXTENSIONS

@app.route(&#39;/&#39;, methods=<span class="cp">[</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;POST&#39;</span><span class="cp">]</span>)
def upload_file():
    if request.method == &#39;POST&#39;:
        file = request.files<span class="cp">[</span><span class="s1">&#39;file&#39;</span><span class="cp">]</span>
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config<span class="cp">[</span><span class="s1">&#39;UPLOAD_FOLDER&#39;</span><span class="cp">]</span>, filename))
            return redirect(url_for(&#39;uploaded_file&#39;,
                                    filename=filename))
    return &#39;&#39;&#39;
    <span class="cp">&lt;!doctype html&gt;</span>
    <span class="nt">&lt;title&gt;</span>Upload new File<span class="nt">&lt;/title&gt;</span>
    <span class="nt">&lt;h1&gt;</span>Upload new File<span class="nt">&lt;/h1&gt;</span>
    <span class="nt">&lt;form</span> <span class="na">action=</span><span class="s">&quot;&quot;</span> <span class="na">method=</span><span class="s">post</span> <span class="na">enctype=</span><span class="s">multipart/form-data</span><span class="nt">&gt;</span>
      <span class="nt">&lt;p&gt;&lt;input</span> <span class="na">type=</span><span class="s">file</span> <span class="na">name=</span><span class="s">file</span><span class="nt">&gt;</span>
         <span class="nt">&lt;input</span> <span class="na">type=</span><span class="s">submit</span> <span class="na">value=</span><span class="s">Upload</span><span class="nt">&gt;</span>
    <span class="nt">&lt;/form&gt;</span>
    &#39;&#39;&#39;
</pre></div>


<p>**EDIT
I just noticed that the files are being uploaded properly to the server but for some reason it still gives an error.</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5871_creator_details">
                            
                                
                            
                            jwebapp3
                            |
                            3
                            posts
                            |
                            
                        </span>
                        <span id="id_post_5871_created_date">
                            Aug. 12, 2013, 6:45 a.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/828/#id_post_5871">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5874" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5874_content_text" class="span8">
                        <p>Well, the upload itself looks fine, and then the result of the POST operation is a redirect to another URL (which is standard practice for POSTs, to avoid issues reloading the subsequent page and getting browser warnings about resubmitting forms). However, the target of the redirect is <code>url_for('uploaded_file')</code> and I can't see an <code>uploaded_file</code> endpoint in your script - do you mean <code>url_for('upload_file')</code>?</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5874_creator_details">
                            
                                
                                    <img src="/static/anywhere/images/star.gif" alt="Beloved premium user" title="Beloved premium user"/>
                                
                            
                            Cartroo
                            |
                            638
                            posts
                            |
                            
                        </span>
                        <span id="id_post_5874_created_date">
                            Aug. 12, 2013, 8:43 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/828/#id_post_5874">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5878" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5878_content_text" class="span8">
                        <p>Oops. That's what I meant. Thanks!</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5878_creator_details">
                            
                                
                            
                            jwebapp3
                            |
                            3
                            posts
                            |
                            
                        </span>
                        <span id="id_post_5878_created_date">
                            Aug. 13, 2013, 3:31 a.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/828/#id_post_5878">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5880" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5880_content_text" class="span8">
                        <p>Hi jwebapp3, have you checked your error logs? They'll usually tell you what exception your application is raising when you are uploading a file. </p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5880_creator_details">
                            
                                <img src="/static/anywhere/images/staff.png" alt="Staff" title="Staff"/>
                            
                            hansel
                            |
                            435
                            posts
                            |
                            
                                PythonAnywhere staff
                                |
                            
                        </span>
                        <span id="id_post_5880_created_date">
                            Aug. 13, 2013, 10:58 a.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/828/#id_post_5880">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    

    




        <div id="id_footer" class="container">

            <footer id="id_copyright_div">
                <p>
                    Copyright &copy; 2013 PythonAnywhere LLP
                    &mdash;
                    <a href="/terms/">Terms</a>
                    &mdash;
                    <a href="/privacy/">Privacy</a><br/>
                    "Python" is a registered trademark of the Python Software Foundation.
                </p>
            </footer>

        </div>

        
        

        <div id="id_feedback_dialog" title="Help us improve" style="display:none">
    <div id="id_feedback_dialog_blurb_big" class="dialog_blurb_big">
        It's always a pleasure to hear from you!
    </div>
    <div id="id_feedback_dialog_blurb_small">
        Ask us a question, or tell us what you love or hate about PythonAnywhere.<br/>
        We'll get back to you over email ASAP.
    </div>
    <textarea id="id_feedback_dialog_text" rows="6"></textarea>
    <input id="id_feedback_dialog_email_address" type="text" class="default_prompt"/>
    <div id="id_feedback_dialog_error" class="pa_hidden">
        Sorry, there was an error connecting to the server. <br/>Please try again in a few moments...
    </div>
    <div class="dialog_buttons">
        <img id="id_feedback_dialog_spinner" src="/static/anywhere/images/spinner-small.gif" />
        <button class="btn btn-primary" id="id_feedback_dialog_ok_button">OK</button>
        <button class="btn" id="id_feedback_dialog_cancel_button">Cancel</button>
    </div>
</div>


        <!-- Le javascript
        ================================================== -->
        <!-- Placed at the end of the document so the pages load faster -->
        <script type="text/javascript" src="/static/jquery/jquery-1.7.1.min.js"></script>
        <script type="text/javascript" src="/static/jquery/jquery-ui-1.8.11.custom.min.js"></script>
        <script src="/static/bootstrap/js/bootstrap-transition.js"></script>
        <script src="/static/bootstrap/js/bootstrap-alert.js"></script>
        <script src="/static/bootstrap/js/bootstrap-modal.js"></script>
        <script src="/static/bootstrap/js/bootstrap-dropdown.js"></script>
        <script src="/static/bootstrap/js/bootstrap-scrollspy.js"></script>
        <script src="/static/bootstrap/js/bootstrap-tab.js"></script>
        <script src="/static/bootstrap/js/bootstrap-tooltip.js"></script>
        <script src="/static/bootstrap/js/bootstrap-popover.js"></script>
        <script src="/static/bootstrap/js/bootstrap-button.js"></script>
        <script src="/static/bootstrap/js/bootstrap-collapse.js"></script>
        <script src="/static/bootstrap/js/bootstrap-carousel.js"></script>
        <script src="/static/bootstrap/js/bootstrap-typeahead.js"></script>

        <script type="text/javascript">
            var urls = {};
            var Anywhere = {};
            Anywhere.urls = {};
        </script>
        <script type="text/javascript" src="/static/anywhere/scripts/ajax_utils.js"></script>

        <script type="text/javascript" src="/static/anywhere/scripts/feedback_dialog.js"></script>
        
        <script type="text/javascript">
            $(function() {
                $.extend(
                    urls,
                    {
                        feedback: '/feedback/submit/'
                    }
                );
                
                    Anywhere.FeedbackDialog.Initialise(urls, false);
                
            });
        </script>

        <script type="text/javascript">

            var _gaq = _gaq || [];
            _gaq.push(['_setAccount', 'UA-18014859-6']);
            _gaq.push(['_trackPageview']);

            (function() {
                var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
            })();

        </script>

        



    </body>
</html>
